package cn.icanci.loopstack.bic.dal.mongo.mongo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.UnitViewTreeDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.UnitViewTreeDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/17 15:49
 */
@Service("unitViewTreeDAO")
public class MongoUnitViewTreeDAO extends AbstractBaseDAO<UnitViewTreeDO> implements UnitViewTreeDAO {
    @Override
    public void insert(UnitViewTreeDO unitViewTreeDO) {
        super.insert(unitViewTreeDO);
        mongoTemplate.insert(unitViewTreeDO, COLLECTION_NAME);
    }

    @Override
    public void update(UnitViewTreeDO unitViewTreeDO) {
        super.update(unitViewTreeDO);
        mongoTemplate.save(unitViewTreeDO, COLLECTION_NAME);
    }

    @Override
    public List<UnitViewTreeDO> queryAll() {
        Criteria criteria = Criteria.where(UnitViewTreeColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public PageList<UnitViewTreeDO> pageQuery(UnitViewTreeDO unitViewTreeDO, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(UnitViewTreeColumn.ENV).is(DEFAULT_ENV);
        if (StringUtils.isNotBlank(unitViewTreeDO.getSystemUuid())) {
            criteria.and(UnitViewTreeColumn.SYSTEM_UUID).is(unitViewTreeDO.getSystemUuid());
        }
        if (StringUtils.isNotBlank(unitViewTreeDO.getUseCaseUuid())) {
            criteria.and(UnitViewTreeColumn.USE_CASE_UUID).is(unitViewTreeDO.getUseCaseUuid());
        }
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, UnitViewTreeColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    @Override
    public UnitViewTreeDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(UnitViewTreeColumn._ID).is(_id);
        criteria.and(UnitViewTreeColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return UnitViewTreeColumn.class;
    }

    @Override
    public UnitViewTreeDO queryBySystemUuidAndUseCaseUuid(String systemUuid, String useCaseUuid) {
        Criteria criteria = Criteria.where(UnitViewTreeColumn.USE_CASE_UUID).is(useCaseUuid);
        criteria.and(UnitViewTreeColumn.ENV).is(DEFAULT_ENV);
        criteria.and(UnitViewTreeColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }
}
